package com.turing.post.thirdinterface.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.cardmanagement.util.ActivateCard;
import com.turing.post.cardmanagement.util.ActivateCardDao;
import com.turing.post.thirdinterface.util.OffLineDetailCHKBusiness;
import com.turing.post.thirdinterface.util.ThirdInterfaceApplicationException;
import com.turing.post.thirdinterface.util.ThirdInterfaceRespCode;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 * 脱机流水校验业务实现
 * 
 * @author YangShuo
 * 
 */

@Stateless(mappedName = "thirdinterface.impl.OffLineDetailCHKBusinessImpl")
@Remote
@Local
public class OffLineDetailCHKBusinessImpl implements OffLineDetailCHKBusiness {

//	@EJB(mappedName = "cardtransactions.impl.CardTransBusinessImpl")
//	private CardTransBusiness CardTransBusinessImpl;

	@EJB(mappedName = "cardmanagement.impl.ActivateCardDaoImpl")
	private ActivateCardDao activateCardDaoImpl;

	/**
	 * 校验卡号是否合法
	 * 
	 * @param cardno
	 *            卡号
	 * @return b；true代表通过 false代表不通过
	 */
	@Override
	public boolean chkNoLength(String cardno) throws PCPSApplicationException {
		boolean b = false;
		// 传入值为空，返回false
		if (PCPSUtil.isEmpty(cardno))
			return false;
		// 用卡号做查询，如能查到信息证明卡号正确返回true
		ActivateCard ac = activateCardDaoImpl.getActivateCardByCardNo(cardno);
		if (ac != null) {
			b = true;
		}
		return b;
	}

	/**
	 * 校验金额是否合法
	 * 
	 * @param amount
	 *            金额
	 * @return b；true代表通过 false代表不通过
	 */
	@Override
	public boolean chkAmount(long amount) throws PCPSApplicationException {

		return amount > 0 ? true : false;
	}

	/**
	 * 校验日期和时间是否合法
	 * 
	 * @param Date
	 *            日期
	 * @param Time
	 *            时间
	 * @param trandate
	 *            对账日期
	 * @return b；true代表通过 false代表不通过
	 */
	@Override
	public boolean chkDateAndTime(String Date, String Time, Date trandate)
			throws PCPSApplicationException {
		boolean b = false;
		// 将时间与日期合并
		String datetime = Date + " " + Time;
		// 将日期转换成date型，如转换失败说明日期和时间错误
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
		try {
			Date d = formatter.parse(datetime);
			if (formatter.format(d).substring(0, 8).equals(Date))
				b = true;
		} catch (Exception e) {
			// TODO: handle exception
			throw new ThirdInterfaceApplicationException(
					ThirdInterfaceApplicationException.ERRCODE_CATEGORY_APPLICATION,
					ThirdInterfaceRespCode.TranDate_ERR, "交易日期格式有误!");
		}
		return b;
	}
}
